package com.xjm.api.admin;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.xjm.entity.vo.AdminVo;
import com.xjm.entity.vo.LoginAdminVo;
import com.xjm.entity.vo.Result;
import com.xjm.enums.StatusEnum;
import com.xjm.service.AdminService;
import com.xjm.service.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/common")
@RequiredArgsConstructor
public class CommonController {

    private final AdminService adminService;

    private final MenuService menuService;


    /**
     * 获取当前登录的最新状态
     */
    @GetMapping("/getInfo")
    public Result<LoginAdminVo> getInfo() {
        LoginAdminVo loginAdminVo = new LoginAdminVo();
        if (StpUtil.isLogin()) {
            AdminVo adminById = adminService.getAdminById(StpUtil.getLoginIdAsInt());
            BeanUtil.copyProperties(adminById, loginAdminVo);
            loginAdminVo.setMenus(menuService.getAdminList(adminById.getId()));
            loginAdminVo.setToken(StpUtil.getTokenValue());
            return Result.success(loginAdminVo);
        }
        return Result.error(StatusEnum.Unauthorized);
    }
}
